﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using System.Windows.Forms;
using Microsoft.Office.Interop.Word;
using Methodes.WordTemplate1;

namespace WordTemplate1.Objets
{
    /// <summary>
    /// Element de type champs
    /// </summary>
    public class EField : Element
    {
        #region Variables


        private string _nomChamp = String.Empty;

        public string NomChamp
        {
            get { return _nomChamp; }
            set { _nomChamp = value; }
        }

        /// <summary>
        /// Valeur du champs dans TFS
        /// </summary>
        private string _valueTFS = String.Empty;

        /// <summary>
        /// Ref Name du champs dans TFS
        /// </summary>
        private string _referenceName = string.Empty;


        /// <summary>
        /// Name du champs dans TFS
        /// </summary>
        private string _name = string.Empty;


        /// <summary>
        /// Name du champs dans TFS
        /// </summary>
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        /// <summary>
        /// Ref Name du champs dans TFS
        /// </summary>
        public string ReferenceName
        {
            get { return _referenceName; }
            set { _referenceName = value; }
        }

        /// <summary>
        /// Valeur du champs dans TFS
        /// </summary>
        public string ValueTFS
        {
            get { return _valueTFS.Replace("\n", "\r"); }
            set { _valueTFS = value.Replace("\n", "\r"); }
        }

        /// <summary>
        /// Valeur du champs dans le document
        /// </summary>
        public string ValueDocument { get; set; }


        #endregion Variables


        public EField(Microsoft.TeamFoundation.WorkItemTracking.Client.Field field)
            : base()
        {

            this.Type = TypeElement.Field;
            this.Tree = new TreeNode(field.Name, 1, 1);
            this.Tree.Tag = this;
            this.Workitem = field.WorkItem;
            this._referenceName = field.ReferenceName;
            this._nomChamp = field.ReferenceName;

            this.ReadOnly = !field.IsEditable;

            if (!this.ReadOnly)
                this.ReadOnly = Contents.GetReadOnly(field.ReferenceName);

            WdContentControlType wcct = WdContentControlType.wdContentControlText;

            if (field.Value != null)
            {
                this.ValueTFS = field.Value.ToString().Replace("\n", "\r");

                if (field.Value.GetType() is System.DateTime)
                {
                    this.wdContentControl = WdContentControlType.wdContentControlDate;
                }

                if (field.Value.GetType() is System.Boolean)
                {
                    this.wdContentControl = WdContentControlType.wdContentControlCheckBox;
                }
            }
        }


        public override string ToString()
        {
            return "La propriété (" + this._nomChamp + ") de l'élément ID : " + this.Workitem.Id.ToString() + " a été modifiée";
        }

        /// <summary>
        /// Permet de noter de changement de donnée
        /// </summary>
        public void Modify(ContentControl value)
        {
            if (value.Range.Text.Trim().ToUpperInvariant().CompareTo(this.ValueTFS.Trim().ToUpperInvariant()) != 0)
            {
                if (!this.ReadOnly)
                {
                    this.IsModify = true;

                    this.ValueDocument = value.Range.Text;

                    if (this.ContentcontrolsId.Contains(value.ID))
                    {
                        this.AddContentControl(value);
                        ActualiseFieldInDocument();
                    }

                    Globals.ThisDocument.taskInformation.ActualiseInformation();
                }
            }
        }


        public static void ActualiseFieldInDocument()
        {
            var elements = from search in Globals.ThisDocument.taskPanel.ListElement
                           where search.InDocument > 0
                           && search.IsModify
                           select search;

            Globals.ThisDocument.taskPanel.lstDonneeInDocument.DataSource = elements.ToList();
            Globals.ThisDocument.taskPanel.lstDonneeInDocument.DisplayMember = "Field.Name";

        }


        /// <summary>
        /// Sauvegarde des données
        /// </summary>
        public void Save()
        {
            this.Workitem.Open();

            if (this.Workitem.IsValid())
            {
                this.Workitem.Fields[this.ReferenceName].Value = this.ValueDocument;
                this.ValueTFS = this.ValueDocument;
                this.Workitem.Save();

                this.Workitem.Save();

                this.IsModify = false;

            }
        }
    }
}
